package org.example.doctorapi.util;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.doctorapi.data.BaseDto;
import org.example.doctorapi.data.SysDictData;
import org.example.doctorapi.data.SysLoginInfo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class IdGeneration<T extends BaseDto> {
    private long workerId = 1; //第几号机房
    private long datacenterId = 1; //第几号机器
    private Snowflake snowflake = IdUtil.getSnowflake(workerId, datacenterId);

    public Page<T> GetRecords(T sysDictData, QueryWrapper<T> queryWrapper, BaseMapper<T> mapper){
        List<T> sysDictDataList = mapper.selectList(queryWrapper);
        Page<T> sysDictDataPage = new Page<>();
        sysDictDataPage.setSize(sysDictData.getPageSize());
        sysDictDataPage.setCurrent(sysDictData.getPageNum());
        sysDictDataPage.setTotal(sysDictDataList.size());
        sysDictDataPage.setRecords(sysDictDataList);
        return sysDictDataPage;
    }

    public static Integer generateVerificationCode(){
        Random random = new Random();
        return random.nextInt(9000) + 1000;
    }

    public synchronized Long snowflakeIdShow(){
        return snowflake.nextId();
    }

    /**
     * post请求方法
     * @param url 请求路径
     * @param body 请求结构体
     * @return 方法请求结果
     */
    public static String post(String url,String body){
        System.out.println("body:"+System.lineSeparator()+body);
        String result = "";
        try {
            OutputStreamWriter out =null;
            BufferedReader in = null;
            URL readUrl = new URL(url);
            URLConnection connection = readUrl.openConnection();
            // 设置连接参数
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(20000);
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            // 提交参数
            out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
            out.write(body);
            out.flush();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(),StandardCharsets.UTF_8));
            String line = "";
            Boolean firstLine = true;
            while ((line = in.readLine()) != null){
                if (firstLine){
                    firstLine = false;
                }else {
                    result += System.lineSeparator();
                }
                result += line;
            }
        }catch (Exception e){
            System.out.println(e);
        }
        return result;
    }
    // hutool
    public JSONObject hutoolPost(String url,HashMap<String, Object> body){
        String result = HttpUtil.post(url, JSONObject.toJSONString(body));
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println("jsonObject:"+jsonObject);
        if (jsonObject.getInteger("error") == 0){
            return jsonObject;
        }
        return null;
    }

}
